package cn.zn.modules.money.cur.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.zn.framework.common.exception.RRException;
import cn.zn.modules.money.cur.dao.TMoneyInfoDetailFreezsMapper;
import cn.zn.modules.money.cur.entity.TMoneyInfoDetailFreezs;
import cn.zn.modules.money.cur.entity.dto.TMoneyInfoDetailFreezeDTO;
import cn.zn.modules.money.cur.entity.vo.MoneyAvailableMoneyVO;
import cn.zn.modules.money.cur.service.TMoneyInfoDetailFreezsService;
import cn.zn.modules.money.cur.service.TMoneyInfoService;
import cn.zn.modules.money.inter.MoneyInfoDetailFreezsInter;
import cn.zn.modules.pz.inter.TpMoneyInter;
import cn.zn.modules.pz.inter.dto.TpMoneyDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class TMoneyInfoDetailFreezsServiceImpl extends ServiceImpl<TMoneyInfoDetailFreezsMapper, TMoneyInfoDetailFreezs> implements TMoneyInfoDetailFreezsService, MoneyInfoDetailFreezsInter {

    @Autowired
    private TMoneyInfoService tMoneyInfoService;
    @Autowired
    private TpMoneyInter tpMoneyInter;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {
        // 删除明细
        tpMoneyInter.delete(id);
        TMoneyInfoDetailFreezs tMoneyInfoDetailFreezs = super.getById(id);
        // 更新预算库
        tMoneyInfoService.updateFreeze(tMoneyInfoDetailFreezs.getProjectCode(), tMoneyInfoDetailFreezs.getCateCode(), NumberUtil.sub(Double.valueOf(0), tMoneyInfoDetailFreezs.getMoney()));
        super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(TMoneyInfoDetailFreezeDTO dto) {
        // 判断是否超出预算
        MoneyAvailableMoneyVO moneyAvailableMoneyVO = tMoneyInfoService.queryAvailableMoney(dto.getProjectCode(), dto.getCateCode());
        if (NumberUtil.compare(dto.getMoney(), moneyAvailableMoneyVO.getAvailableMoney()) > 0) {
            // 申请金额大于可用金额
            throw new RRException("已超预算!");
        }
        // 保存冻结
        TMoneyInfoDetailFreezs tMoneyInfoDetailFreezs = new TMoneyInfoDetailFreezs();
        BeanUtil.copyProperties(dto, tMoneyInfoDetailFreezs);
        tMoneyInfoDetailFreezs.setId(null);
        tMoneyInfoDetailFreezs.setState(1);
        tMoneyInfoDetailFreezs.setCateCode(moneyAvailableMoneyVO.getCateCode());
        super.save(tMoneyInfoDetailFreezs);
        // 保存明细
        TpMoneyDTO tpeMoneyDTO = new TpMoneyDTO();
        BeanUtil.copyProperties(dto, tpeMoneyDTO);
        tpeMoneyDTO.setMoneyFreezeId(tMoneyInfoDetailFreezs.getId());
        tpMoneyInter.save(tpeMoneyDTO);
        // 更新预算库
        tMoneyInfoService.updateFreeze(tMoneyInfoDetailFreezs.getProjectCode(), tMoneyInfoDetailFreezs.getCateCode(), dto.getMoney());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TMoneyInfoDetailFreezeDTO dto) {
        // 判断是否超出预算
        Double compareMoney = 0D;
        MoneyAvailableMoneyVO moneyAvailableMoneyVO = tMoneyInfoService.queryAvailableMoney(dto.getProjectCode(), dto.getCateCode());
        TMoneyInfoDetailFreezs tMoneyInfoDetailFreezs = super.getById(dto.getId());
        if (StrUtil.equals(tMoneyInfoDetailFreezs.getProjectCode(), dto.getProjectCode()) && StrUtil.equals(tMoneyInfoDetailFreezs.getCateCode(), moneyAvailableMoneyVO.getCateCode())) {
            // 同预算，判断多出部分是否超预算
            compareMoney = NumberUtil.sub(dto.getMoney(), tMoneyInfoDetailFreezs.getMoney());
        } else {
            compareMoney = dto.getMoney();
        }
        if (NumberUtil.compare(compareMoney, 0D) > 0 && NumberUtil.compare(compareMoney, moneyAvailableMoneyVO.getAvailableMoney()) > 0) {
            // 申请金额大于可用金额
            throw new RRException("已超预算!");
        }
        // 更新冻结
        TMoneyInfoDetailFreezs tMoneyInfoDetailFreezsSave = new TMoneyInfoDetailFreezs();
        BeanUtil.copyProperties(dto, tMoneyInfoDetailFreezsSave);
        tMoneyInfoDetailFreezsSave.setCateCode(moneyAvailableMoneyVO.getCateCode());
        super.updateById(tMoneyInfoDetailFreezsSave);
        // 更新明细
        TpMoneyDTO tpeMoneyDTO = new TpMoneyDTO();
        BeanUtil.copyProperties(dto, tpeMoneyDTO);
        tpeMoneyDTO.setMoneyFreezeId(tMoneyInfoDetailFreezsSave.getId());
        tpeMoneyDTO.setId(null);
        tpMoneyInter.update(tpeMoneyDTO);
        // 更新预算库
        tMoneyInfoService.updateFreeze(tMoneyInfoDetailFreezs.getProjectCode(), tMoneyInfoDetailFreezs.getCateCode(), NumberUtil.sub(Double.valueOf(0), tMoneyInfoDetailFreezs.getMoney()));
        tMoneyInfoService.updateFreeze(tMoneyInfoDetailFreezsSave.getProjectCode(), tMoneyInfoDetailFreezsSave.getCateCode(), tMoneyInfoDetailFreezsSave.getMoney());
    }

    @Override
    public List<TMoneyInfoDetailFreezs> queryFreezsApply(String moneyInfoId, Map<String, Object> params) {
        TMoneyInfoDetailFreezs tMoneyInfoDetailFreezs = BeanUtil.mapToBean(params, TMoneyInfoDetailFreezs.class, true);
        return baseMapper.queryFreezsApply(moneyInfoId, tMoneyInfoDetailFreezs);
    }
}
